package www.openteam.com.adapter.policy;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.bean.SendRcvUserMsg;

public class SendRcvApprlAdp extends RecyclerView.Adapter<SendRcvApprlAdp.ViewHolder> {
    Context context;
    List<SendRcvUserMsg.DataBeanX.DataBean> dataBeanList = new ArrayList<>();
    List<Boolean> isflag = new ArrayList<>();
    String[] intArray;
    boolean ischeck;
    SendRcvApprlAdpClick sendRcvApprlAdpClick;
    SendRcvApprlAdpAudit sendRcvApprlAdpAudit;

    public void setIscheck(boolean ischeck) {
        this.ischeck = ischeck;
    }

    public interface SendRcvApprlAdpClick {
        void postData(int postion);
    }

    public interface SendRcvApprlAdpAudit {
        void postData(int postion, Button button);
    }

    public void setSendRcvApprlAdpAudit(SendRcvApprlAdpAudit sendRcvApprlAdpAudit) {
        this.sendRcvApprlAdpAudit = sendRcvApprlAdpAudit;
    }

    public SendRcvApprlAdp(Context context, List<SendRcvUserMsg.DataBeanX.DataBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
        int size = dataBeanList.size();
        for (int i = 0; i < size; i++) {
            if (i == 0) {
                isflag.add(i, true);
            } else {
                isflag.add(i, false);
            }
        }
        intArray = context.getResources().getStringArray(R.array.approvalStatus01);
//        context.getResources().getStringArray()
    }

    public void setSendRcvApprlAdpClick(SendRcvApprlAdpClick sendRcvApprlAdpClick) {
        this.sendRcvApprlAdpClick = sendRcvApprlAdpClick;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_sendrcv_apprl, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    public void changeData(int change, int statue) {
        dataBeanList.get(change).setStatus(statue);
    }

    public void changeData(int change, int statue,String checker,String checkdate) {
        dataBeanList.get(change).setStatus(statue);
        dataBeanList.get(change).setCheckdate(checkdate);
        dataBeanList.get(change).setChecker(checker);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        SendRcvUserMsg.DataBeanX.DataBean dataBean = dataBeanList.get(i);
        viewHolder.srcvApprlDate.setText(dataBean.getSenddate());
//        viewHolder.srcvApprlDate.setText(dataBean.getRdsno());
        viewHolder.srcvApprlFactory.setText(dataBean.getOtfactory());
        viewHolder.srcvApprlSender.setText(dataBean.getSender());
        viewHolder.srcvApprlRdsno.setText(dataBean.getRdsno());
        viewHolder.srcvApprlMnryName.setText(dataBean.getMoneyName());
        viewHolder.srcvApprlSummoryMnry.setText(cleanDecimal(dataBean.getSummoney()));
        final int status = dataBean.getStatus();
        viewHolder.srcvApprlStatus.setText(intArray[status]);
        viewHolder.aCard.setCardBackgroundColor(context.getResources().getColor(R.color.gray));
        if (status == 1) {
            viewHolder.aCard.setCardBackgroundColor(context.getResources().getColor(R.color.red_shallow));
            viewHolder.btnAuditing.setText("取消审核");
        } else if (status == 0) {
            viewHolder.aCard.setCardBackgroundColor(context.getResources().getColor(R.color.green));
            viewHolder.btnAuditing.setText("审核");
        }
        if (isflag.get(i)) {
            viewHolder.aCard.setCardBackgroundColor(context.getResources().getColor(R.color.actionbar_background));
        }
        viewHolder.lineitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sendRcvApprlAdpClick != null) {
                    int size = isflag.size();
                    for (int i = 0; i < size; i++) {
                        isflag.set(i, false);
                    }
                    isflag.set(i, true);
                    notifyDataSetChanged();
                    sendRcvApprlAdpClick.postData(i);
                }
            }
        });
        viewHolder.btnAuditing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sendRcvApprlAdpAudit != null) {
                    sendRcvApprlAdpAudit.postData(i, viewHolder.btnAuditing);
                }
            }
        });
    }

    public String cleanDecimal(String text) {
        Double num = Double.valueOf(text.isEmpty() ? "0" : text);
        NumberFormat nf = NumberFormat.getInstance();
        return nf.format(num);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.srcv_apprl_date)
        TextView srcvApprlDate;
        @Bind(R.id.btn_auditing)
        Button btnAuditing;
        @Bind(R.id.srcv_apprl_factory)
        TextView srcvApprlFactory;
        @Bind(R.id.srcv_apprl_sender)
        TextView srcvApprlSender;
        @Bind(R.id.srcv_apprl_rdsno)
        TextView srcvApprlRdsno;
        @Bind(R.id.lineitem)
        LinearLayout lineitem;
        @Bind(R.id.a_card)
        CardView aCard;
        @Bind(R.id.srcv_apprl_status)
        TextView srcvApprlStatus;
        @Bind(R.id.srcv_apprl_summoryMnry)
        TextView srcvApprlSummoryMnry;
        @Bind(R.id.srcv_apprl_MnryName)
        TextView srcvApprlMnryName;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
